/***************************************************************************
 创建者: hualei
 开始时间: 2020-09-29
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2020.9.29 类的具体实现代码编写

 ***************************************************************************
 *  CHARGING 充电指令具体实现                                                                       *
 ***************************************************************************/
#ifndef CHARGINGINSTRUCTIONLOGIC_H
#define CHARGINGINSTRUCTIONLOGIC_H

#include "instructionEnum.h"
#include "charginginstruction.h"
#include "GeneralDefine.h"
#include <QTimer>
#include <QObject>
#include <QMap>
#include "fleetdefine.h"

class ProgramSet;
class ProgramEngine;
class RobotIo;
class MoveFreeInstructionLogic;

struct ChargeConfig
{
    DPoint point;
    double speedRatio;
    double allowError;
};

class ChargingInstructionLogic:public QObject
{
    Q_OBJECT

public:

    /**
     * @brief ChargingInstructionLogic              充电指令实现类构造函数
     * @param robotIdIn                             机器编号
     * @param programSetIn                          程序集合
     * @param programEngineIn                       程序解释引擎
     */
    ChargingInstructionLogic(int robotIdIn, ProgramSet* programSetIn,ProgramEngine* programEngineIn,RobotIo *ioControllerIn
                             ,MoveFreeInstructionLogic * moveFreeIn);

public:

    /**
     * @brief startCharging                 开始执行充电指令
     * @param programNumIn                  程序编号
     * @param instructionIn                 充电指令
     * @param lineIndex                     指令行号
     * @return                              程序指令状态
     */
    E_PROGRAM_LOOP_RESULT startCharging(int programNumIn,const ChargingInstruction& instructionIn,
                                        int lineIndex);
    E_PROGRAM_LOOP_RESULT charging_run(const ChargingInstruction& instructionIn,int lineIndex);
    E_PROGRAM_LOOP_RESULT charging_run(const ChargeParameter& paraIn);


private:
    E_PROGRAM_LOOP_RESULT moveToChargePosition(bool isPositiveRun,
                                               int pointIndexIn, int lineIndexIn, int detectBarrierMethodIn);

    int readConfigFile(QString fileName);

    void qtTimeSleep_second(int second);

    int chargeDeviceOn();

    int chargeDeviceOff();

    /**
     * @brief addMsg                添加消息
     * @param messageLevel          消息级别
     * @param componentName
     * @param messageType
     * @param messageCode
     * @param robotId
     * @param parameter1
     * @param parameter2
     * @param parameter3
     * @param parameter4
     * @param message
     */
    void addMsg( int messageLevel,
                 std::string componentName,
                 std::string messageType,
                 int messageCode,
                 int robotId,
                 int parameter1 = 0,
                 int parameter2 = 0,
                 int parameter3 = 0,
                 int parameter4 = 0,
                 QString message= "");


public slots:

    /**
     * @brief timerOutSlot      定时器超时响应槽函数
     */
    void timerOutSlot();

private:
    /**
     * @brief robotId                   机器编号
     */
    int robotId;

    /**
     * @brief programSet                指令程序集合对象
     */
    ProgramSet* programSet;

    /**
     * @brief programEngine             程序解释引擎对象
     */
    ProgramEngine* programEngine;

    /**
     * @brief batteryInformation        电池组状态信息
     */
    struct BatteryInformation batteryInformation;

    /**
     * @brief timer                     定时器
     */
    QTimer timer;

    /**
     * @brief currentChargingType       当前充电类型
     */
    EM_CHARGING_FINISH_TYPE currentChargingType;

    /**
     * @brief currentChargingInstruction        当前充电指令
     */
    ChargingInstruction currentChargingInstruction;

    /**
     * @brief ioControllerIn    io控制对象
     */
    RobotIo *ioController;

     int defaultChargeType;
     int barrierDetectMethod;
     int charge_do_index;
     int charge_di_index;
     QMap<int,ChargeConfig> chargePositionList;
     MoveFreeInstructionLogic * moveFree;
     int chargeCurrencyWaitTime;
     double charge_minCurrency;//最小充电电流，带正负号，默认为正
     double charge_maxCurrency;//最大充电电流，带正负号，默认为正
     bool isInitialStatus;
};

#endif // CHARGINGINSTRUCTIONLOGIC_H
